package com.itheima.goods.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.constant.StatusConstant;
import com.itheima.exception.BaseException;
import com.itheima.exception.DeletionNotAllowedException;
import com.itheima.goods.dto.DishDTO;
import com.itheima.goods.dto.DishPageQueryDTO;
import com.itheima.goods.mapper.DishFlavorMapper;
import com.itheima.goods.mapper.DishMapper;
import com.itheima.goods.mapper.SetmealDishMapper;
import com.itheima.goods.pojo.Dish;
import com.itheima.goods.pojo.DishFlavor;
import com.itheima.goods.pojo.SetmealDish;
import com.itheima.goods.service.DishService;

import com.itheima.goods.vo.DishVO;
import com.itheima.result.PageBean;
import com.itheima.utils.TextAutoUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;
import java.util.Set;

@Service
@Slf4j
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public PageBean select(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Page<DishVO> dishes = dishMapper.pageselect(dishPageQueryDTO);

        return new PageBean(dishes.getTotal(), dishes.getResult());
    }
    /**
     * 菜品起售、停售
     *
     * @param status
     * @param id
     * @return
     */
    @Override
    public void updateStatus(Integer status, Long id) {

        //菜品停售，关联的套餐也停售
        dishMapper.updateStatus(status, id);
        //如果该菜品是停售的那么关联的套餐也带是停售
        Long st=dishMapper.selectStatus(id);
        if (st==0){
            setmealDishMapper.updateStatus(id);
        }
        Set keys = redisTemplate.keys(RedisConstant.DISH_LIST + "*");
        redisTemplate.delete(keys);
    }

    @Override
    public DishVO findById(Long id) {

        DishVO dishVO = dishMapper.findById(id);
        return dishVO;
    }

    @Override
    public List<Dish> listDish(Long categoryId) {
        List<Dish> dishes = dishMapper.listDish(categoryId);
        return dishes;
    }
    //添加菜品，描述有内容审核
    @Override
    @Transactional
    public void add(DishDTO dishDTO) {
        String description = dishDTO.getDescription();
        TextAutoUtil textAutoUtil = new TextAutoUtil();
        String s = textAutoUtil.checkText(description);

            if (!Objects.equals(s, "")){

                throw new BaseException(s);
            }

        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dish.setStatus(StatusConstant.DISABLE);
        dishMapper.addDish(dish);
        Long id = dish.getId();
        List<DishFlavor> flavors = dishDTO.getFlavors();
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(id);
            dishMapper.addByDishById(flavor);
        }
        Set keys = redisTemplate.keys(RedisConstant.DISH_LIST + dishDTO.getCategoryId());
        redisTemplate.delete(keys);
    }

    @Override
    @Transactional
    public void delete(Long[] ids) {
        //判断菜品下有没有还在售卖
        List<Dish> dishes=dishMapper.selectByIds(ids);
        if (dishes.size()>0){
            throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
        }
        //判断菜品上有套餐吗
        List<SetmealDish> list=   setmealDishMapper.selectByIds(ids);
        if (list.size()>0){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        dishMapper.deleteByIds(ids);
        Long[] dishId=ids;
        dishFlavorMapper.deleteByDishId(dishId);
        Set keys = redisTemplate.keys(RedisConstant.DISH_LIST +"*");
        redisTemplate.delete(keys);
    }

    @Override
    @Transactional
    public void updateDish(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.updateDish(dish);
        Long dishId = dishDTO.getId();
        dishFlavorMapper.deleteById(dishId);
        List<DishFlavor> flavors = dishDTO.getFlavors();
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dishId);
            dishMapper.addByDishById(flavor);
        }
        Set keys = redisTemplate.keys(RedisConstant.DISH_LIST + "*");
        redisTemplate.delete(keys);
    }
//==============================
    @Override
    public List<DishVO> fidByCategoryId(Long categoryId) {

        String redisData= RedisConstant.DISH_LIST +categoryId;
        List<DishVO> dishVOS = (List<DishVO>) redisTemplate.opsForValue().get(redisData);
        if (dishVOS!=null){
            return dishVOS;
        }

        dishVOS= dishMapper.selectByCategoryId(categoryId);
       log.info("{}  ",dishVOS);
       redisTemplate.opsForValue().set(redisData,dishVOS);
        return dishVOS;
    }

    @Override
    public Dish findByDishId(Long id) {
        Dish dish = dishMapper.findByDishId(id);
        return dish;
    }
}
